package com.sjc.algorithm.interview;

import java.util.Stack;

/**
 * 用2个栈模拟队列
 * @author by SangJiacun
 * @Date 2020/5/13 22:55
 */
public class Stack2Queue {
    private static Stack<Integer> stackA = new Stack<>();
    private static Stack<Integer> stackB = new Stack<>();

    public static void main(String[] args) {
        Stack2Queue stackQueue = new Stack2Queue();
        stackQueue.enQueue(1);
        stackQueue.enQueue(2);
        stackQueue.enQueue(3);

        System.out.println(stackQueue.deQueue());
        System.out.println(stackQueue.deQueue());

        stackQueue.enQueue(4);
        System.out.println(stackQueue.deQueue());
        System.out.println(stackQueue.deQueue());
    }

    private void enQueue(int element){
        stackA.push(element);
    }

    private Integer deQueue(){
        if(stackB.empty()){
            if(stackA.empty()){
                return null;
            }
            stactA2StackB();
        }
        return stackB.pop();
    }

    private void stactA2StackB(){
        while(!stackA.empty()){
            stackB.push(stackA.pop());
        }
    }

}
